package lambda.reduce;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

public class Test1
{
	public static void main(String[] args)
	{
		List<Student> list =new ArrayList<>();
		list.add(new Student("",10,"1"));
		list.add(new Student("",18,"1"));
		list.add(new Student("",11,"2"));
		
		
		Map<String, Integer> sumByClass = list.stream()
			    .collect(Collectors.groupingBy(Student::getClassName, 
			             Collectors.reducing(0, Student::getAge, Integer::sum)));
		
		System.out.println(sumByClass);
	}
	
	public static void test() {
//		

	}
	
	
	public static void test1() {
		List<Student> list =new ArrayList<>();
		list.add(new Student("",10,"1"));
		list.add(new Student("",18,"1"));
		list.add(new Student("",11,"2"));
		Optional<Student> oldest = list.stream()
			    .collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Student::getAge))));
	}
	
	
}
class Student{
	private String name="";
	private int age=0;
	private String className="";
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	
	public String getClassName()
	{
		return className;
	}
	public void setClassName(String className)
	{
		this.className = className;
	}
	public Student(String name, int age)
	{
		super();
		this.name = name;
		this.age = age;
	}
	public Student(String name, int age, String className)
	{
		super();
		this.name = name;
		this.age = age;
		this.className = className;
	}
	
	
	
	
}